package dao.imp;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import model.Order;
import dao.DaoHelper;
import dao.OrderDao;

public class OrderDaoImp implements OrderDao {

	private static OrderDaoImp orderDao = new OrderDaoImp();
	private static DaoHelper daoHelper=DaoHelperImp.getBaseDaoInstance();

	private OrderDaoImp(){}

	public static OrderDaoImp getInstance()
	{
		return orderDao;
	}

	@Override
	public boolean save(Order order) {
		// TODO Auto-generated method stub
		Connection con=daoHelper.getConnection();
		PreparedStatement stmt=null;
		boolean success = true;

		try {	
			stmt=con.prepareStatement("insert into orders value(?, ?, ?, ?, ?, ?, ?)");
			stmt.setInt(1, order.getId());
			stmt.setString(2, order.getCustomer());
			stmt.setDate(3, order.getDate());
			stmt.setInt(4, order.getGoodsId());
			stmt.setString(5, order.getGoodsName());
			stmt.setInt(6, order.getQuantity());
			stmt.setFloat(7, order.getPrice());
			stmt.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			success = false;
		} finally {
			daoHelper.closeConnection(con);
			daoHelper.closePreparedStatement(stmt);
		}

		return success;
	}

	@Override
	public List<Order> findAll() {
		// TODO Auto-generated method stub
		Connection con=daoHelper.getConnection();
		PreparedStatement stmt=null;
		ResultSet result=null;
		int i = 0;
		String c = null;
		Date d = null;
		int g = 0;
		String n = null;
		int q = 0;
		float p = 0;
		Order order = null;
		ArrayList<Order> list = new ArrayList<Order>();

		try {	
			stmt=con.prepareStatement("select * from orders");
			result=stmt.executeQuery();
			while (result.next()) {
				i = result.getInt(1);
				c = result.getString(2);
				d = result.getDate(3);
				g = result.getInt(4);
				n = result.getString(5);
				q = result.getInt(6);
				p = result.getFloat(7);
				order = new Order(i, c, d, g, n, q, p);
				list.add(order);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			daoHelper.closeConnection(con);
			daoHelper.closePreparedStatement(stmt);
			daoHelper.closeResult(result);
		}

		return list;
	}

	@Override
	public List<Order> findByCustomer(String customer) {
		// TODO Auto-generated method stub
		Connection con=daoHelper.getConnection();
		PreparedStatement stmt=null;
		ResultSet result=null;
		int i = 0;
		String c = null;
		Date d = null;
		int g = 0;
		String n = null;
		int q = 0;
		float p = 0;
		Order order = null;
		ArrayList<Order> list = new ArrayList<Order>();

		try {	
			stmt=con.prepareStatement("select * from orders where customer = ?");
			stmt.setString(1, customer);
			result=stmt.executeQuery();
			while (result.next()) {
				i = result.getInt(1);
				c = result.getString(2);
				d = result.getDate(3);
				g = result.getInt(4);
				n = result.getString(5);
				q = result.getInt(6);
				p = result.getFloat(7);
				order = new Order(i, c, d, g, n, q, p);
				list.add(order);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			daoHelper.closeConnection(con);
			daoHelper.closePreparedStatement(stmt);
			daoHelper.closeResult(result);
		}

		return list;
	}

	@Override
	public Order findById(int id) {
		Connection con=daoHelper.getConnection();
		PreparedStatement stmt=null;
		ResultSet result=null;
		int i = 0;
		String c = null;
		Date d = null;
		int g = 0;
		String n = null;
		int q = 0;
		float p = 0;
		Order order = null;

		try {	
			stmt=con.prepareStatement("select * from orders where id = ?");
			stmt.setInt(1, id);
			result=stmt.executeQuery();
			while (result.next()) {
				i = result.getInt(1);
				c = result.getString(2);
				d = result.getDate(3);
				g = result.getInt(4);
				n = result.getString(5);
				q = result.getInt(6);
				p = result.getFloat(7);
				order = new Order(i, c, d, g, n, q, p);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			daoHelper.closeConnection(con);
			daoHelper.closePreparedStatement(stmt);
			daoHelper.closeResult(result);
		}

		return order;
	}

	@Override
	public boolean removeByCustomer(String customer) {
		// TODO Auto-generated method stub
		Connection con=daoHelper.getConnection();
		PreparedStatement stmt=null;
		boolean success = true;

		try {	
			stmt=con.prepareStatement("delete from orders where customer = ?");
			stmt.setString(1, customer);
			stmt.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			success = false;
		} finally {
			daoHelper.closeConnection(con);
			daoHelper.closePreparedStatement(stmt);
		}

		return success;
	}

	@Override
	public boolean removeById(int id) {
		// TODO Auto-generated method stub
		Connection con=daoHelper.getConnection();
		PreparedStatement stmt=null;
		boolean success = true;

		try {	
			stmt=con.prepareStatement("delete from orders where id = ?");
			stmt.setInt(1, id);
			stmt.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			success = false;
		} finally {
			daoHelper.closeConnection(con);
			daoHelper.closePreparedStatement(stmt);
		}

		return success;
	}

	@Override
	public boolean updateById(Order order) {
		// TODO Auto-generated method stub
		Connection con=daoHelper.getConnection();
		PreparedStatement stmt=null;
		boolean success = true;

		try {	
			stmt=con.prepareStatement("update orders set quantity = ?, price = ? where id = ?");
			stmt.setInt(1, order.getQuantity());
			stmt.setFloat(2, order.getPrice());
			stmt.setInt(3, order.getId());
			stmt.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			success = false;
		} finally {
			daoHelper.closeConnection(con);
			daoHelper.closePreparedStatement(stmt);
		}

		return success;
	}

}
